﻿
// MFCApplication1_QTDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "MFCApplication1_QT.h"
#include "MFCApplication1_QTDlg.h"
#include "afxdialogex.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// shiftData函数用于将新数据插入到数组中，同时将旧数据向前移动
static void shiftData(double *data, int len, double newValue)
{
	// 将数组中的数据向前移动一位
	memmove(data, data + 1, sizeof(double) * (len - 1));
	// 将新值插入到数组的最后一个位置
	data[len - 1] = newValue;
}


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CMFCApplication1QTDlg 对话框



CMFCApplication1QTDlg::CMFCApplication1QTDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MFCAPPLICATION1_QT_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCApplication1QTDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_SHOW_PIC_VIEW, m_ChartViewer);  // 关联CChartViewer控件
}

BEGIN_MESSAGE_MAP(CMFCApplication1QTDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &CMFCApplication1QTDlg::OnBnClickedOk)
	ON_CONTROL(CVN_ViewPortChanged, IDC_STATIC_SHOW_PIC_VIEW, OnViewPortChanged)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CMFCApplication1QTDlg 消息处理程序

BOOL CMFCApplication1QTDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	// 初始化CChartViewer控件

	// 初始化背景颜色
	m_extBgColor = getDefaultBgColor();
	// 初始化数据数组
	for (int i = 0; i < sampleSize; ++i)
		m_timeStamps[i] = m_dataSeriesA[i] = m_dataSeriesB[i] = m_dataSeriesC[i] = Chart::NoValue;

	// 设置初始时间
	SYSTEMTIME st;
	GetLocalTime(&st);
	m_nextDataTime = Chart::chartTime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute,
		st.wSecond) + st.wMilliseconds / 1000.0;

	// 设置定时器
	SetTimer(DataRateTimer, DataInterval, 0);  // 数据采集定时器
	SetTimer(ChartUpdateTimer, 100, 0);  // 图表更新定时器



	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CMFCApplication1QTDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMFCApplication1QTDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCApplication1QTDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMFCApplication1QTDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}


void CMFCApplication1QTDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	switch (nIDEvent)
	{
	case DataRateTimer:
		// 数据采集定时器 - 获取新数据样本
		getData();
		break;
	case ChartUpdateTimer:
		// 图表更新定时器 - 请求图表更新
		m_ChartViewer.updateViewPort(true, false);
		break;
	}
	CDialogEx::OnTimer(nIDEvent);
}
void CMFCApplication1QTDlg::getData()
{
	// 获取当前时间
	SYSTEMTIME st;
	GetLocalTime(&st);
	double now = Chart::chartTime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute,
		st.wSecond) + st.wMilliseconds / 1000.0;

	// 生成随机数据
	do
	{
		double p = m_nextDataTime * 4;
		double dataA = 20 + cos(p * 129241) * 10 + 1 / (cos(p) * cos(p) + 0.01);
		double dataB = 150 + 100 * sin(p / 27.7) * sin(p / 10.1);
		double dataC = 150 + 100 * cos(p / 6.7) * cos(p / 11.9);

		// 将新数据移入数组
		shiftData(m_dataSeriesA, sampleSize, dataA);
		shiftData(m_dataSeriesB, sampleSize, dataB);
		shiftData(m_dataSeriesC, sampleSize, dataC);
		shiftData(m_timeStamps, sampleSize, m_nextDataTime);

		m_nextDataTime += DataInterval / 1000.0;
	} while (m_nextDataTime < now);
}

void CMFCApplication1QTDlg::drawChart(CChartViewer *viewer)
{
	// 创建一个XYChart对象
	XYChart *c = new XYChart(600, 270, 0xf4f4f4, 0x000000, 1);
	c->setRoundedFrame(m_extBgColor);

	// 设置绘图区
	c->setPlotArea(55, 62, 520, 175, 0xffffff, -1, -1, 0xcccccc, 0xcccccc);
	c->setClipping();

	// 添加标题
	c->addTitle("实时数据曲线", "timesbi.ttf", 15)->setBackground(0xdddddd, 0x000000, Chart::glassEffect());

	// 添加图例框
	LegendBox *b = c->addLegend2(55, 33, 3, "arialbd.ttf", 9);
	b->setBackground(Chart::Transparent, Chart::Transparent);
	b->setWidth(520);

	// 配置y轴
	c->yAxis()->setTitle("数值", "arialbd.ttf", 10);

	// 配置x轴
	c->xAxis()->setTickDensity(75, 15);
	c->xAxis()->setWidth(2);
	c->yAxis()->setWidth(2);

	// 添加数据到图表
	double lastTime = m_timeStamps[sampleSize - 1];
	if (lastTime != Chart::NoValue)
	{
		c->xAxis()->setDateScale(lastTime - DataInterval * sampleSize / 1000, lastTime);
		c->xAxis()->setLabelFormat("{value|hh:nn:ss}");

		LineLayer *layer = c->addLineLayer();
		layer->setXData(DoubleArray(m_timeStamps, sampleSize));

		char buffer[1024];

		sprintf_s(buffer, sizeof(buffer), "Alpha: <*bgColor=FFCCCC*> %.2f ", m_dataSeriesA[sampleSize - 1]);
		layer->addDataSet(DoubleArray(m_dataSeriesA, sampleSize), 0xff0000, buffer);

		sprintf_s(buffer, sizeof(buffer), "Beta: <*bgColor=CCFFCC*> %.2f ", m_dataSeriesB[sampleSize - 1]);
		layer->addDataSet(DoubleArray(m_dataSeriesB, sampleSize), 0x00cc00, buffer);

		sprintf_s(buffer, sizeof(buffer), "Gamma: <*bgColor=CCCCFF*> %.2f ", m_dataSeriesC[sampleSize - 1]);
		layer->addDataSet(DoubleArray(m_dataSeriesC, sampleSize), 0x0000ff, buffer);
	}

	// 将图表设置到WinChartViewer
	viewer->setChart(c);
	delete c;
}

int CMFCApplication1QTDlg::getDefaultBgColor()
{
	LOGBRUSH LogBrush;
	HBRUSH hBrush = (HBRUSH)SendMessage(WM_CTLCOLORDLG, (WPARAM)CClientDC(this).m_hDC,
		(LPARAM)m_hWnd);
	::GetObject(hBrush, sizeof(LOGBRUSH), &LogBrush);
	int ret = LogBrush.lbColor;
	return ((ret & 0xff) << 16) | (ret & 0xff00) | ((ret & 0xff0000) >> 16);
}

void CMFCApplication1QTDlg::OnViewPortChanged()
{
	drawChart(&m_ChartViewer);
}